<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="/lib/layui/css/layui.css">
</head>
<body>
  <div style="margin: 10px;">
    <span class="layui-breadcrumb">
      <a href="/dashboard">首页</a>
      <a href="/cateList">分类列表</a>
      <a><cite>添加分类</cite></a>
    </span>
    <div style="max-width:600px;margin-top:20px">
      <form class="layui-form" action="">

        <input type="hidden" name="_id" value="<%= cate._id %>"/>
        <div class="layui-form-item">
          <label class="layui-form-label">分类名称</label>
          <div class="layui-input-block">
            <input type="text" name="cateName"
              value="<%= cate.cateName %>"
              required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">父级分类</label>
          <div class="layui-input-block">
            <select name="pid" lay-verify="required">
              <option value=""></option>
              <option value="0" <%= cate.pid == 0 ? 'selected': '' %>>顶级分类</option>
              <% for(var i=0;i<cates.length;i++){ %>
                <option <%= cate.pid == cates[i]._id ? 'selected' :'' %> value="<%= cates[i]._id %>">
                  <%= cates[i].cateName %>
                </option>
              <%}%>
            </select>
          </div>
        </div>
       
        <div class="layui-form-item">
          <label class="layui-form-label">是否可用</label>
          <div class="layui-input-block">
            <input type="checkbox" <%= cate.cateState == 1 ? 'checked': '' %> name="cateState" lay-skin="switch">
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">分类图标</label>
          <div class="layui-input-block">
            <button type="button" class="layui-btn" id="upload">
              <i class="layui-icon">&#xe67c;</i>上传图片
            </button>
            <img src="<%= cate.cateIcon?cate.cateIcon:'' %>" width="150" class="img" alt="">
          </div>
        </div>
        <div class="layui-form-item layui-form-text">
          <label class="layui-form-label">分类描述</label>
          <div class="layui-input-block">
            <textarea name="cateDesc" 
              placeholder="请输入内容" 
              class="layui-textarea">
              <%= cate.cateDesc %>
            </textarea>
          </div>
        </div>
        <div class="layui-form-item">
          <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
          </div>
        </div>
      </form>
    </div>
  </div>
  <script src="/lib/layui/layui.js"></script>
  <script>
    layui.use(['element', 'jquery', 'form', 'upload','layer'], function () {
      var element = layui.element;
      var $ = layui.jquery;
      var form = layui.form;
      var upload = layui.upload;
      var layer = layui.layer;
      var cateIcon="";

      
      //监听提交
      form.on('submit(formDemo)', function(data){
        // 处理 分类 是否可用状态
        let field = data.field;
        if(field.cateState === 'on'){
          field.cateState = 1
        }else{
          field.cateState = 0
        }
        field.cateIcon = cateIcon
        delete field.file;
        console.log(field);
        // 提交
        $.ajax({
          url:"/cateEdit",
          type:'POST',
          dataType:'json',
          data:field,
          success:function(res){
            if(res.code === 0){
              // 成功
              layer.msg(res.msg,{icon:1},function(){
                  location.href="/cateList";
              })
            }else{
              // 失败
              layer.msg(res.msg,{icon:2},function(){
                history.go(0)
              })
            }
          }

        }) 
        return false;
      });
      // 上传文件
      var uploadInst = upload.render({
        elem: '#upload' //绑定元素
        ,url: '/upload' //上传接口
        ,field:'file'
        ,done: function(res){
          //上传完毕回调
          console.log(res)
          cateIcon = res.data.filePath;
          $('.img').attr('src',res.data.filePath)
        }
        ,error: function(err){
          //请求异常回调
          console.log(err)
        }
      });
     
    })
  </script>
</body>
</html>
